package com.adcc.ags.gms.gw.service;

import com.adcc.ags.gms.core.entity.GWLogLevel;
import com.adcc.ags.gms.core.util.Constant;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.gw.entity.ServiceInfo;
import com.adcc.ags.gms.gw.mapper.ServiceMapper;
import com.adcc.ags.gms.gw.vo.AMQSMonitorVO;
import com.adcc.ags.gms.gw.vo.GWMonitorVO;
import com.adcc.ags.gms.gw.vo.MQInfoMonitorVO;
import com.adcc.ags.gms.mq.service.QMService;
import com.adcc.ags.gms.mq.vo.QMVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * Created by zhangpeng on 2016/10/13.
 */
@Service("SysMonitorService")
public class SysMonitorServiceImpl implements SysMonitorService {

    @Autowired
    private ServiceMapper serviceMapper;

    @Autowired
    private LogFactory log;

    @Autowired
    private SysConfigurationService sysConfigurationService;

    @Autowired
    private QMService qmService;

    @Override
    public Optional<Date> findDBDateTime() {
        try {
            return Optional.of(serviceMapper.findDBDateTime());
        } catch (Exception ex) {
            log.error(SysMonitorService.class, "SysMonitor", GWLogLevel.ALARM, "findDBDateTime() error", ex);
            return Optional.absent();
        }
    }

    @Override
    public Optional<Long> getServiceCount() {
        try {
            return Optional.of(serviceMapper.getCount());
        } catch (Exception ex) {
            log.error(SysMonitorService.class, "SysMonitor", GWLogLevel.ALARM, "getServiceCount() error", ex);
            return Optional.absent();
        }
    }

    @Override
    public Optional<Boolean> saveService(ServiceInfo serviceInfo) {
        try {
            serviceMapper.insert(serviceInfo);
            return Optional.of(true);
        } catch (Exception ex) {
            log.error(SysMonitorService.class, "SysMonitor", GWLogLevel.ALARM, "saveService() error", ex);
            return Optional.absent();
        }
    }

    @Override
    public Optional<Boolean> deleteALLService() {
        try {
            serviceMapper.deleteAll();
            return Optional.of(true);
        } catch (Exception ex) {
            log.error(SysMonitorService.class, "SysMonitor", GWLogLevel.ALARM, "deleteAll() error", ex);
            return Optional.absent();
        }
    }

    @Override
    public Optional<List<ServiceInfo>> findAllService() {
        try {
            return Optional.of(serviceMapper.findAll());
        } catch (Exception ex) {
            log.error(SysMonitorService.class, "SysMonitor", GWLogLevel.ALARM, "findAllService() error", ex);
            return Optional.absent();
        }
    }

    @Override
    public Optional<AMQSMonitorVO> getAmqsMonitor() {
        AMQSMonitorVO amqsMonitorVO = new AMQSMonitorVO();
        try {
            Map<String, LinkedHashMap<String, String>> map = UserMonitorServiceImpl.getRestService(sysConfigurationService.getValue(Constant.USER_MONITOR_URL));
            if (map != null) {
                LinkedHashMap<String, String> gwMap = map.get("amqs");
                Map<String, Map<String, String>> map1 = new ObjectMapper().readValue(gwMap.get("message"), Map.class);

                Map<String, String> mqMap = map1.get("mq");
                amqsMonitorVO.setChannel(mqMap.get("channel"));
                amqsMonitorVO.setPort(mqMap.get("port"));
                amqsMonitorVO.setHost(mqMap.get("host"));
                amqsMonitorVO.setState(mqMap.get("state"));
                amqsMonitorVO.setQm(mqMap.get("qm"));
            }
            return Optional.of(amqsMonitorVO);
        } catch (IOException e) {
            log.error(SysMonitorService.class, "SysMonitor", GWLogLevel.ALARM, "getAmqsMonitor() error", e);
            return Optional.absent();
        }
    }

    @Override
    public Optional<MQInfoMonitorVO> getMqMonitor() {
        MQInfoMonitorVO mqInfoMonitorVO = new MQInfoMonitorVO();
        try {
            Optional<List<QMVO>> optional = qmService.getAllQMByPCF();
            if (optional.isPresent() && optional.get().size() > 0) {
                QMVO qmvo = new QMVO();
                qmvo = optional.get().get(0);
                mqInfoMonitorVO.setHost(qmvo.getIp());
                mqInfoMonitorVO.setName(qmvo.getName());
                mqInfoMonitorVO.setConnectCount(qmvo.getConnectCount());
                mqInfoMonitorVO.setPort(qmvo.getPort());
                mqInfoMonitorVO.setState(qmvo.getStatus());
                mqInfoMonitorVO.setStartTime(qmvo.getStartTime());
            }
            return Optional.of(mqInfoMonitorVO);
        } catch (Exception e) {
            log.error(SysMonitorService.class, "SysMonitor", GWLogLevel.ALARM, "getMqMonitor() error", e);
            return Optional.absent();
        }
    }

    //    @Override
//    public Optional<GWMonitorVO> getGWMonitor() {
//        //结果集
//        GWMonitorVO gwMonitorVO = new GWMonitorVO();
//        try {
//            Map<String, LinkedHashMap<String, String>> map = UserMonitorServiceImpl.getRestService(sysConfigurationService.getValue(Constant.USER_MONITOR_URL));
//            if (map != null) {
//                LinkedHashMap<String, String> gwMap = map.get("gw");
//                Map<String, Map<String, String>> map1 = new ObjectMapper().readValue(gwMap.get("message"), Map.class);
//
//                Map<String, String> mqMap = map1.get("mq");
//                gwMonitorVO.setChannel(mqMap.get("channel"));
//                gwMonitorVO.setPort(mqMap.get("port"));
//                gwMonitorVO.setHost(mqMap.get("host"));
//                gwMonitorVO.setState(mqMap.get("state"));
//                gwMonitorVO.setQm(mqMap.get("qm"));
//
//                String mode = new ObjectMapper().writeValueAsString(map1.get("mode"));
//                gwMonitorVO.setMode(mode.replace("\"", ""));
//            }
//            return Optional.of(gwMonitorVO);
//        } catch (Exception e) {
//            Log.error(SysMonitorService.class.getName(), "getGWMonitor() error", e);
//            return Optional.absent();
//        }
//
//    }
    @Override
    public Optional<GWMonitorVO> getGWMonitor() {
        //结果集
        GWMonitorVO gwMonitorVO = new GWMonitorVO();
        try {
            Map<String, LinkedHashMap<String, String>> map = UserMonitorServiceImpl.getRestService(sysConfigurationService.getValue(Constant.USER_MONITOR_URL));
            if (map != null) {
                LinkedHashMap<String, String> gwMap = map.get("gw");
                Map<String, Map<String, String>> map1 = new ObjectMapper().readValue(gwMap.get("message"), Map.class);
                Boolean healthy = Boolean.valueOf(new ObjectMapper().writeValueAsString(gwMap.get("healthy")));

                Map<String, String> mqMap = map1.get("mq");
                //GW 名称  IP
                String gwName = new ObjectMapper().writeValueAsString(map1.get("name")).replace("\"", "");
                String gwHost = new ObjectMapper().writeValueAsString(map1.get("host")).replace("\"", "");
                // name ip 匹配上
                if ((gwName.equals(sysConfigurationService.getValue(Constant.GW_MASTER_NAME)) && gwHost.equals(sysConfigurationService.getValue(Constant.GW_MASTER_IP))) || (gwName.equals(sysConfigurationService.getValue(Constant.GW_SLAVE_NAME)) && gwHost.equals(sysConfigurationService.getValue(Constant.GW_SLAVE_IP)))) {
                    gwMonitorVO.setName(gwName);
                    gwMonitorVO.setIp(gwHost);
                    gwMonitorVO.setChannel(mqMap.get("channel"));
                    gwMonitorVO.setPort(mqMap.get("port"));
                    gwMonitorVO.setHost(mqMap.get("host"));
                    gwMonitorVO.setState(mqMap.get("state"));
                    gwMonitorVO.setHealthy(healthy);
                    gwMonitorVO.setQm(mqMap.get("qm"));
                    String mode = new ObjectMapper().writeValueAsString(map1.get("mode"));
                    gwMonitorVO.setMode(mode.replace("\"", ""));
                }
            } else {//CGW服务没有开启一异常情况(默认显示主用)
                gwMonitorVO.setName(sysConfigurationService.getValue(Constant.GW_MASTER_NAME));
                gwMonitorVO.setIp(sysConfigurationService.getValue(Constant.GW_MASTER_IP));
            }
            return Optional.of(gwMonitorVO);
        } catch (Exception e) {
            log.error(SysMonitorService.class, "SysMonitor", GWLogLevel.ALARM, "getGWMonitor() error", e);
            return Optional.absent();
        }

    }
}
